﻿using Siasun.AFC.RtPlatfromGui.UI.BusinessHelper;
using Siasun.AFC.RtPlatfromGui.UI.ViewModel.BaseViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Timers;
using Siasun.AFC.RtPlatfromGui.UI.Command;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using Siasun.AFC.RtPlatfromGui.IBll.Model;
using System.Collections.ObjectModel;
using Siasun.AFC.RtPlatfromGui.UI.Model;
using Siasun.AFC.RtPlatfromGui.IBll.IServer;

namespace Siasun.AFC.RtPlatfromGui.UI.ViewModel
{
    public class PlatfromWinMainViewModel : WinBaseViewModel
    {
        #region Bind 属性
        private Frame _detailPageFrame;
        private ObservableCollection<MenuTreeInfo> menuTreeList;

        public Frame DetailPageFrame
        {
            get
            {
                return _detailPageFrame;
            }

            set
            {
                _detailPageFrame = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 菜单树集合
        /// </summary>
        public ObservableCollection<MenuTreeInfo> MenuTreeList
        {
            get
            {
                return menuTreeList;
            }

            set
            {
                menuTreeList = value;
                RaisePropertyChanged();
            }
        }
        #endregion

        #region Command 命令
        public RelayCommand<object> ShowPageCommand
        {
            get { return new RelayCommand<object>((t)=>ShowPageCommandExecte(t)); }
            set { ShowPageCommand = value; }
        }

        public RelayCommand<object> ClosingPageCommand
        {
            get { return new RelayCommand<object>((obj) => ClosingPageCommandExecte(obj)); }
        }
        #endregion

        #region 构造函数
        public PlatfromWinMainViewModel()
        {
            if (IsInDesignMode)
            {
                #region 测试数据
                var createMenuList = new ObservableCollection<MenuTreeInfo>();
                createMenuList.Add(new MenuTreeInfo
                {
                    MenuName = "测试",
                    IsCanUse = true,
                    MenuTag = new MenuInfo()
                    {
                        MenuName = "测试"
                    },
                    ChildrenList = new ObservableCollection<MenuTreeInfo>{
                         new MenuTreeInfo() {
                         MenuName = "测试3",
                         IsCanUse = true,
                         MenuTag = new MenuInfo() {
                            MenuName = "测试3"
                            },
                         }
                    }
                });
                createMenuList.Add(new MenuTreeInfo
                {
                    MenuName = "测试2",
                    IsCanUse = false,
                    MenuTag = new MenuInfo()
                    {
                        MenuName = "测试2"
                    },
                });
                MenuTreeList = createMenuList;
                #endregion
            }
        }
        #endregion

        #region 方法
        public override void InitConfig()
        {
            this.DetailPageFrame = new Frame();

            Messenger.Default.Register<string>(this, "ShowPage", MessengerShowPage);

            //获取根元素树
            MenuTreeList = MenuDataHelper.GetInstanse().GetMenuTreeForUse();
        }

        public override void InitLayout()
        {
            //throw new NotImplementedException();
        }

        public override void InitVoice()
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 消息传递打开菜单
        /// </summary>
        /// <param name="pageNameString"></param>
        public void MessengerShowPage(string pageNameString)
        {
            if (!ShowFrameDetailPage(pageNameString))
            {
                Console.WriteLine("菜单显示失败！");
            }
        }

        /// <summary>
        /// 显示菜单
        /// </summary>
        /// <param name="pageNameString">菜单类的名称</param>
        /// <returns></returns>
        public bool ShowFrameDetailPage(string pageNameString)
        {
            if (!LoadingPage(pageNameString)) return false;

            DetailPageFrame.Navigate(DetailPageListHelper.DFindPage(pageNameString));
            //DetailPageListHelper.DFindPage(pageNameString).ParentUserWindow = this;
            App.DoEvents();

            while (DetailPageFrame.NavigationService.CanGoBack)
            {
                DetailPageFrame.NavigationService.RemoveBackEntry();
            }
            return true;
        }

        /// <summary>
        /// 显示菜单
        /// </summary>
        /// <param name="pageNameString">菜单类的名称</param>
        /// <returns></returns>
        public bool ShowFrameDetailPage(MenuInfo menuNodeInfo)
        {
            if (!LoadingPage(menuNodeInfo)) return false;

            DetailPageFrame.Navigate(DetailPageListHelper.DFindPage(menuNodeInfo.MenuPageName));
            //DetailPageListHelper.DFindPage(pageNameString).ParentUserWindow = this;
            App.DoEvents();

            while (DetailPageFrame.NavigationService.CanGoBack)
            {
                DetailPageFrame.NavigationService.RemoveBackEntry();
            }
            return true;
        }

        /// <summary>
        /// 加载菜单，对于初始化时间比较长的菜单可以提前加载
        /// </summary>
        /// <param name="pageNameString">菜单类的名称</param>
        /// <returns></returns>
        public bool LoadingPage(string pageNameString)
        {
            if (!DetailPageListHelper.DConainKey(pageNameString))
            {
                PageUI.DetailBasePage detailPage
                    = Gui.Base.Context.ObjectContainer.GetServerObject<PageUI.DetailBasePage>(pageNameString);
                if (object.Equals(null, detailPage)) return false;
                //判断是否返回的默认值，如果是默认值可以记录日志创建菜单失败

                DetailPageListHelper.DListAdd(pageNameString, detailPage);
                return true;
            }
            return true;
        }

        /// <summary>
        /// 加载菜单，对于初始化时间比较长的菜单可以提前加载
        /// </summary>
        /// <param name="pageNameString">菜单类的名称</param>
        /// <returns></returns>
        public bool LoadingPage(MenuInfo menuNodeInfo)
        {
            if (object.Equals(null, menuNodeInfo) || string.IsNullOrEmpty(menuNodeInfo.MenuPageName))
            {
                return false;
            }

            if (!DetailPageListHelper.DConainKey(menuNodeInfo.MenuPageName))
            {
                PageUI.DetailBasePage detailPage
                    = Gui.Base.Context.ObjectContainer.GetServerObject<PageUI.DetailBasePage>(
                        menuNodeInfo.MenuPageName,
                        menuNodeInfo.DllFilePath + menuNodeInfo.DllName,
                        menuNodeInfo.NamespacePath
                        );
                if (object.Equals(null, detailPage)) return false;
                //判断是否返回的默认值，如果是默认值可以记录日志创建菜单失败

                DetailPageListHelper.DListAdd(menuNodeInfo.MenuPageName, detailPage);
                return true;
            }
            return true;
        }
        #endregion

        #region 定时器方法
        public override void AsynStatusTimer_Tick(object sender, ElapsedEventArgs e)
        {
            throw new NotImplementedException();
        } 
        #endregion

        #region 事件
        public override void LoadPageInitLayout(object obj)
        {
            //初始化参数
            this.InitConfig();
            //初始化界面
            this.InitLayout();
            //发送语音
            InitVoice();
        }

        public override void UnLoadPageOper(object obj)
        {
            Messenger.Default.Unregister(this);
        }

        public void ShowPageCommandExecte(object obj)
        {
            //string objStr = obj.ToString();
            if (object.Equals(null, obj) || !(obj is MenuTreeInfo)) return;

            ShowFrameDetailPage((obj as MenuTreeInfo).MenuTag);
        }

        public void ClosingPageCommandExecte(object obj)
        {
            Application.Current.Shutdown();
            //base.OnClosed(e);
            System.Environment.Exit(0);
        }
        #endregion
    }
}
